#pragma once 

#include<iostream>
#include<memory>
#include<sys/epoll.h>
#include"nocopy.hpp"
#include"Sock.hpp"
#include"log.hpp"
#include "Epoller.hpp"


uint32_t EVENT_IN = (EPOLLIN);
uint32_t EVENT_OUT = (EPOLLOUT);


class EpollServer: public nocopy
{
    static const int num = 64;
public:
    EpollServer(uint16_t port)
        :_port(port),
        _listsocket_ptr(new Sock()),
        _epoller_ptr(new Epoller())
        {}
    
    void Init()
    {
        _listsocket_ptr->Socket();
        _listsocket_ptr->Bind(_listsocket_ptr->Fd(), _port);
        _listsocket_ptr->Listen(_listsocket_ptr->Fd());
        logMessage(NORMAL, "create listen sock success: %d", _listsocket_ptr->Fd());
    }

    void Accepter()
    {
        // 获取了一个新的连接
        std::string clientip;
        uint16_t clientport;
        int sock = _listsocket_ptr->Accept(_listsocket_ptr->Fd(), 
        &clientip, &clientport);

        if(sock > 0)
        {
            //能直接读取吗？不能！
            _epoller_ptr->EpllerUpdate(EPOLL_CTL_ADD, sock, EVENT_IN);
            logMessage(DEBUG, "get a link , client info@ %s:%d", clientip.c_str(), clientport);
        }

    }

    void Recver(int fd)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer)-1);
        if(n>0)
        {
            buffer[n] = 0;
            std::cout<<"get a message : "<<buffer<<std::endl;

            // //write
            // std::string echo_str = "server echo $:";
            // echo_str+=buffer;
            // write(fd, echo_str.c_str(), echo_str.size());
        }
        else if(n == 0)
        {
            logMessage(NORMAL, "client quit, me too, close ");
            _epoller_ptr->EpllerUpdate(EPOLL_CTL_DEL, fd, 0);
            close(fd);
        }
        else
        {
            logMessage(WARNING, "recv error: fd is : %d", fd);
            _epoller_ptr->EpllerUpdate(EPOLL_CTL_DEL, fd, 0);
            close(fd);
        }
    }


    void Dispatcher(struct epoll_event revs[], int num)
    {
        for(int i = 0; i<num;i++)
        {
            uint32_t events = revs[i].events;
            int fd = revs[i].data.fd;

            if(events & EVENT_IN)  //读事件就绪
            {
                if(fd == _listsocket_ptr->Fd())    //监听的事件
                    Accepter();
                else    //不是监听的就是要来连接的事件
                    Recver(fd);
            }
            else if(events & EVENT_OUT)
            {}
            else
            {}
        }
    }


    void Start()
    {
        //将listensock添加到epoll中->listensock和它关系的事件，添加到内核epoll模型中rb_tree
        _epoller_ptr->EpllerUpdate(EPOLL_CTL_ADD, _listsocket_ptr->Fd(), EVENT_IN);   //监听listensock
        struct epoll_event revs[num];

        for(;;)
        {
            int n = _epoller_ptr->EpollerWait(revs, num);
            if(n>0)
            {
                //有事件就绪
                logMessage(DEBUG, "event happend, fd is : %d", revs[0].data.fd);
                Dispatcher(revs, n);
            }
            else if(n == 0)
            {
                logMessage(DEBUG, "time out...");
            }
            else
            {
                logMessage(ERROR, "epoll wait error");
            }

        }
    }

    ~EpollServer()
    {
        _listsocket_ptr->Close();
    }   


private:
    std::shared_ptr<Sock> _listsocket_ptr;
    std::shared_ptr<Epoller> _epoller_ptr;
    uint16_t _port;
};

